import { DeviceEventEmitter } from 'react-native';

import navigator from '../../util/navigator';
import { Dispatch, State, OrderResultProps } from './types';
import { BACK_TO_TAB_NAV, TabId } from '../TabNav/constant';

let dispatch: Dispatch;
let state: State;

export default (d: Dispatch, s: State): void => {
  dispatch = d;
  state = s;
};

export const fetchFailed = (): void => {
  dispatch({ type: 'FETCH_FAILED' });
};

export const setOrderInfo = (value: OrderResultProps): void => {
  dispatch({ type: 'SET_ORDER_INFO', value });
};

export const startCourse = (): void => {
  DeviceEventEmitter.emit(BACK_TO_TAB_NAV, { id: TabId.Course });
  navigator.navigate('TabNav');
};
